package mory.medecin;

import android.app.ListActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import modele.*;

/**
 *
 * @author mbamba
 */
public class MedecinActivity extends ListActivity {

    List<Medecin> listmeds;
    List<Medecin> listmedSelection;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.medecin);
//        ListView med = (ListView) findViewById(R.id.list);
        Intent inter = getIntent();
        String leDep = inter.getStringExtra("leDep");

        listmeds = DAO.getLesMeds(leDep);
        MedAdapter adapter = new MedAdapter(listmeds, this);
        setListAdapter(adapter);

        Button button = (Button) findViewById(R.id.bouton);
        final EditText filtre = (EditText) findViewById(R.id.filter);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                listmedSelection = getMedecin(filtre.getText().toString());
                MedAdapter adapter = new MedAdapter(listmedSelection, getApplicationContext());
                setListAdapter(adapter);
            }

        });

    }

    public List<Medecin> getMedecin(String filtre) {
        List<Medecin> listmedSelection = new ArrayList<Medecin>();
        for (Medecin m : listmeds) {
            if (m.getNom().startsWith(filtre)) {
                listmedSelection.add(m);
            }
        }
        return listmedSelection;
    }

    @Override
    protected
            void onListItemClick(ListView l, View v, int position, long id) {
        //Toast.makeText(getApplicationContext(), "Bonjour " + lesNoms.get(position), Toast.LENGTH_LONG).show();
        String n;
//        if (listmedSelection.isEmpty()) {
            n = listmeds.get(position).getTelephone();
//        } else {
//            n = listmedSelection.get(position).getTelephone();
//        }
        n = "tel:" + n;
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(n));
        startActivity(intent);
    }
}
